/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate', 'table', 'laypage'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let table = layui.table;
    let laypage = layui.laypage;

    // 初始化表格
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        limit: json.totalCount,
        defaultToolbar: ['filter'],
        page: true
    });

    // 初始化表格分页
    laypage.render(zmnPage.laypageOptions(json));

    // 鼠标移出设置通用时长
    $("#serviceMinutes").on("blur", function (e) {
        let serviceMinutes = e.delegateTarget.value
        if (zmnUtil.isEmptyNull(json.faultItem.itemId)) {
            return false;
        }
        if (serviceMinutes && serviceMinutes != '' && !/^0$|^[1-9]\d*$/.test(serviceMinutes)) {
            return "服务时长只能是正整数";
        }
        let url = '/service/item/addEditServiceMinutes.action'
        zmnPage.ajaxSimple(url, {itemId: json.faultItem.itemId, serviceMinutes: serviceMinutes}, 1000);
    });

    table.on("edit(zmn-table)", function (obj) {
        let data = obj.data
        if (data.serviceMinutes && data.serviceMinutes != '' && !/^0$|^[1-9]\d*$/.test(data.serviceMinutes)) {
            return "服务时长只能是正整数";
        }
        let param = {
            itemId: json.faultItem.itemId,
            companyId: Number(data.companyId),
            serviceMinutes: Number(data.serviceMinutes)
        }
        submitServicetime(param)
    });

    function submitServicetime(data) {
        $.ajax({
            async: false,
            type: "POST",
            url: "/time/servicetime/addEdit.action",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (rs) {
                if (rs.status === 200) {
                    layer.msg(rs.data, {"icon": 1});
                } else {
                    layer.msg(rs.data, {"icon": 2});
                }
            }
        });
    }

    //对外暴露的接口
    exports('serv_service_item_time', {});
});

//显示描述
function show_tips(flag, tips, obj) {
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips, obj, {
            tips: [1, '#3595CC'],
            time: 3000
        })
    }
}
